<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      title="新增员工"
      :visible="showDialog"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <el-form
        ref="addEmployeeRef"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
      >
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="formData.username"
            placeholder="请输入姓名"
            clearable
            :style="{ width: '50%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input
            v-model="formData.mobile"
            placeholder="请输入手机号"
            clearable
            :style="{ width: '50%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="formData.timeOfEntry"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '50%' }"
            placeholder="请选择入职时间"
            clearable
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select
            v-model="formData.formOfEmployment"
            placeholder="请选择聘用形式"
            clearable
            :style="{ width: '50%' }"
          >
            <el-option
              v-for="(item) in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input
            v-model="formData.workNumber"
            placeholder="请输入工号"
            clearable
            :style="{ width: '50%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="formData.departmentName"
            placeholder="请输入部门"
            clearable
            :style="{ width: '50%' }"
            @focus="getDepartments"
          >
          </el-input>
          <!-- 放置一个tree组件 -->
          <el-tree
            v-if="showTree"
            v-loading="loading"
            :data="treeData"
            default-expand-all
            :props="{ label: 'name' }"
            @node-click="selectNode"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="formData.correctionTime"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            :style="{ width: '50%' }"
            placeholder="请选择转正时间"
            clearable
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { hireType } from '@/api/constant/employees' // 映射数组
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      hireType, // 在data中定义数据
      // 表单数据
      treeData: [], // 定义数组接收树形数据
      showTree: false, // 控制树形的显示或者隐藏
      loading: false, // 控制树的显示或者隐藏进度条
      formData: {
        username: undefined,
        mobile: undefined,
        timeOfEntry: null,
        formOfEmployment: undefined,
        workNumber: undefined,
        departmentName: undefined,
        correctionTime: null
      },
      rules: {
        username: [{
          required: true,
          message: '请输入姓名',
          trigger: 'blur'
        }, {
          min: 1,
          max: 4,
          message: '请输入1~4位字符',
          trigger: 'blur'
        }],
        mobile: [{
          required: true,
          message: '请输入手机号',
          trigger: 'blur'
        }, {
          pattern: /^1[3-9]\d{9}$/,
          message: '请输入正确格式的手机号',
          trigger: 'blur'
        }],
        timeOfEntry: [{
          required: true,
          message: '请选择入职时间',
          trigger: 'change'
        }],
        formOfEmployment: [{
          required: true,
          message: '请选择聘用形式',
          trigger: 'change'
        }],
        workNumber: [{
          required: true,
          message: '请输入工号',
          trigger: 'blur'
        }],
        departmentName: [{
          required: true,
          message: '请输入部门',
          trigger: 'change'
        }],
        correctionTime: []
      },
      formOfEmploymentOptions: [{
        'label': '选项一',
        'value': 1
      }, {
        'label': '选项二',
        'value': 2
      }]
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    // 获取部门列表
    async getDepartments() {
      this.showTree = true
      this.loading = true
      const { depts } = await getDepartments() // 获取组织架构
      // depts是数组。不是树型结构
      this.treeData = tranListToTreeData(depts, '')
      this.loading = false
    },
    // 选择部门下拉选项
    selectNode(node) {
      console.log(node)
      this.formData.departmentName = node.name
      this.showTree = false
    },
    onOpen() {},
    // 关闭弹层，调用取消按钮
    onClose() {
      this.close()
    },
    // 取消按钮
    close() {
      // 重置原来的数据
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addEmployeeRef.resetFields() // 重置校验结果
      // 方法一：调用父组件关闭弹层的变量
      // this.$parent.showDialog = false
      // 方法er：父组件里<add-employee :show-dialog.sync="showDialog" />
      this.$emit('update:showDialog', false)
      // 方法三：直接再父组件调用v-if，直接销毁弹层子组件，不用再重置校验
    },
    // 点击弹层表单 确认 按钮
    async handelConfirm() {
      try {
        // 校验整个表单
        await this.$refs['addEmployeeRef'].validate()
        // 调用新增接口
        await addEmployee(this.formData)
        // 告诉父组件更新数据
        this.$parent.getEmployeeList()
        // 告诉父组件 关闭新增弹层
        this.$parent.showDialog = false
        // 提示框,新增成功
        this.$message.success('新增员工成功！')
      } catch (error) {
        console.log(error)
      }
    }
  }
}

</script>
<style>
</style>
